#!/bin/bash
########################################################################
# script name:  sync_passwd.sh
# Used for:     Syncing password everywhere
# Author:       Martin Dumont
# Contributors:
# Version:      0.1
#
# (date format: date +'%Y/%m/%d')
#
# Date          Version Contributors    Modifications
# ----------    ------- ------------    -------------
# 2011/04/06    0.1     MDUMONT         initial
# 2011/04/07    1.o     MDUMONT         official release
########################################################################
usage()
{
cat << STOP

usage: $0 [options] [ARG]

This script is used for ....

OPTIONS:
   -h               Comma seperated list of hosts, or ALL
   -u               Comma seperated list of user name

Ex.:
$0 -h saprmt02 -u k0528817
$0 -h saprmt02,saprmt01 -u k0528817
$0 -h ALL -u k0528817

STOP
}
#######################################
# Get parameters
#######################################
while getopts h:u: OPTION
do
        case $OPTION in
        h)
                HOSTS=$(echo $OPTARG | tr '[A-Z]' '[a-z]' | sed 's/,/ /g')
                ;;
        u)
                USERLIST=$(echo $OPTARG | tr '[A-Z]' '[a-z]' | sed 's/,/ /g')
                ;;
        \?)
                usage
                exit 1
                ;;
        *)
                usage
                exit 1
                ;;
        esac
done
if [ $OPTIND = 1 ]; then usage; exit 1; fi
if [ -z "$USERLIST" ]; then USERLIST=$USER; fi
#######################################
# Functions
#######################################
BAENV=$(dirname `which $0`)/loadenv.sh
if [ -f $BAENV ]; then . $BAENV; else echo "No env library ($BAENV)"; exit 1; fi
SSH_OPTS=" -x -o ConnectTimeout=15 -o BatchMode=yes -o StrictHostKeyChecking=no -o LogLevel=quiet"
#######################################
function askpassword {
	stty -echo
	read -p "Please enter new password: " PASSWORD
	echo
	read -p "Please enter again: " CHECKMATCH
	echo
	stty echo
	if [ "$CHECKMATCH" != "$PASSWORD" ]; then echo -e "\nPasswords are different, exiting."; exit 1; fi
	ENCPASSWORD=$(echo $PASSWORD | openssl passwd -stdin)
}
#######################################
# MAIN
#######################################
askpassword
NEWHOSTLIST=$(checkhost $HOSTS | grep -v Verifying)
for HOSTNAME in $NEWHOSTLIST
do
	for TMPUSER in $USERLIST
	do
		if [ "$TMPUSER" = "root" ]
		then
			echo "Not changing password for root"
		else
			echo -e "Changing password on $HOSTNAME for $TMPUSER ... \c"
			OS=$(sudo -u saunix ssh $SSH_OPTS saunix@$HOSTNAME uname -s)
			if [ "$OS" = "AIX" ]
			then
				sudo -u saunix ssh $SSH_OPTS -T saunix@$HOSTNAME "echo \"$TMPUSER:$ENCPASSWORD\" | sudo /usr/bin/chpasswd -ce && sudo /usr/bin/chuser unsuccessful_login_count=0 $TMPUSER && echo ok" || echo "Login failed"
			elif [ "$OS" = "Linux" ];then
				sudo -u saunix ssh $SSH_OPTS -T saunix@$HOSTNAME "echo "$TMPUSER:$PASSWORD" | sudo /usr/sbin/chpasswd && echo ok" || echo "Login failed"
			else
				echo -e "Machine OS not supported ($OS) or ssh keys not working for user saunix"
			fi
		fi
	done
done
#######################################
exit 0
